package com.zhipu.balance.utils

import android.content.Context
import android.hardware.usb.UsbDevice
import android.hardware.usb.UsbManager
import android.os.Build
import android.view.Gravity
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.brightek.thermallibrary.ThermalService
import com.raizlabs.android.dbflow.config.FlowConfig
import com.raizlabs.android.dbflow.config.FlowLog
import com.raizlabs.android.dbflow.config.FlowManager
import com.zhipu.balance.BaseApplication
import com.zhipu.balance.R
import com.zhipu.balance.common.Constants.LogPath
import com.zhipu.balance.remotedata.AppComponent
import com.zhipu.balance.remotedata.DaggerAppComponent
import com.zhipu.balance.remotedata.SpModule
import com.zhipu.balance.remotedata.systemapi.SystemApiServiceModule
import com.zhipu.balance.utils.OpenSerialPort.initSerialPortOpen
import com.zhipuchina.signature.SignV0

object InitializeUtils {
    /**
     * 上下文
     */
    private var MBaseApplication: BaseApplication? = null
    fun getMBaseApplication(): BaseApplication? {
        return MBaseApplication
    }

    fun setMBaseApplication(baseApplication: BaseApplication) {
        MBaseApplication = baseApplication
    }

    /**
     * Toast设置统一样式
     */
    private var defaultMaker: ToastUtils? = null
    fun getDefaultMaker(): ToastUtils? {
        return defaultMaker
    }
    fun initDefaultMaker() {
        defaultMaker = ToastUtils.getDefaultMaker()
        defaultMaker!!.setBgColor(ColorUtils.getColor(R.color.purple_200))
        defaultMaker!!.setGravity(Gravity.CENTER, 0, 0)
        defaultMaker!!.setTextColor(ColorUtils.getColor(R.color.white))
        defaultMaker!!.setTextSize(36)
    }

    /**
     * 初始化DBFlow
     */
    fun initializeDBFlow(mBaseApplication: BaseApplication) {
        //初始化DBFlow
        FlowManager.init(FlowConfig.builder(mBaseApplication).openDatabasesOnInit(true).build())
        //设置日志显示
        FlowLog.setMinimumLoggingLevel(FlowLog.Level.D)
    }


    /**
     * 日志相关
     */
    fun initLogUtils() {
        val config = LogUtils.getConfig()
        //设置 log 总开关
        config.isLogSwitch = true
        //设置 log 文件开关
        config.isLog2FileSwitch = true
        //设置 log 可保留天数
        config.saveDays = 5
        // 设置 log 文件存储目录
        config.dir = LogPath
    }


    /**
     *  初始化串口
     */
    fun initializingSerialPort(){
        //初始化串口
        initSerialPortOpen()

    }

    /**
     * 初始化
     */
    var appComponent: AppComponent? = null
    fun AppComponent() : AppComponent? {
        return appComponent
    }
    fun initiRetrofit2(baseApplication: BaseApplication){
        appComponent = DaggerAppComponent.builder().systemApiServiceModule(SystemApiServiceModule()).spModule(SpModule(baseApplication)).build()
    }



    var mSignV0 : SignV0? = null
    fun getSignV0(): SignV0? {
        return mSignV0
    }
    /**
     * 初始化签名
     */
    fun initSign(baseApplication: BaseApplication) {
        mSignV0 = SignV0()
        mSignV0?.signatureVerify(baseApplication)
    }


    /**
     * 链接打印机的
     */
    private var thermalService: ThermalService? = null
    fun getThermalService(): ThermalService? {
        return thermalService
    }

    fun setThermalService(thermalService: ThermalService?) {
        this.thermalService = thermalService
    }

    /**
     * 链接打印机
     */
    fun LinkPrinterModel(mContext: Context) {
        val usbDeviceList: MutableList<UsbDevice> = ArrayList()
        val usbManager = mContext.getSystemService(Context.USB_SERVICE) as UsbManager
        val deviceHashMap = usbManager.deviceList
        for (device in deviceHashMap.values) {
            usbDeviceList.add(device)
        }
        for (usbDevice in usbDeviceList) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                if (usbDevice.productName!!.contains("Printer")) {
                    getThermalService()?.usbConns(usbDevice)
                    break
                }
            }
        }
    }





    /**
     * 链接打印机
     */
    private var mLinkPrinter = false
    fun isLinkPrinter(): Boolean {
        return mLinkPrinter
    }
    fun setLinkPrinter(linkPrinter: Boolean) {
        mLinkPrinter = linkPrinter
    }
}



